﻿using System;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

using LTJ.Entity;
using LTJ.Common;
using LTJ.Config;
using LTJ.FrameWork;

namespace LTJ.WebPage.SystemManage.corp
{
    public partial class corpinfogrid : AdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDataGrid();
            }
        }

        protected void BindDataGrid()
        {
            if (ViewState["condition"] == null)
            {
                DataGrid1.VirtualItemCount = Companys.GetCompanyCount("");
                DataGrid1.DataSource = Companys.GetCompanyListByPage(DataGrid1.CurrentPageIndex + 1, DataGrid1.PageSize, "", "");
            }
            else
            {
                DataGrid1.VirtualItemCount = Companys.GetCompanyCount(ViewState["condition"].ToString());
                DataGrid1.DataSource = Companys.GetCompanyListByPage(DataGrid1.CurrentPageIndex + 1, DataGrid1.PageSize, ViewState["condition"].ToString(), "");
            }
            DataGrid1.DataBind();
        }

        protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            DataGrid1.CurrentPageIndex = e.NewPageIndex;
            BindDataGrid();
        }

        protected void GoToPagerButton_Click(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        /// <summary>
        /// 搜索点击
        /// </summary>
        protected void check_Click(object sender, EventArgs e)
        {
            string conditionstr = SASRequest.GetString("corpname");
            if (conditionstr != "" && Utils.IsSafeSqlString(conditionstr))
            {
                ViewState["condition"] = "cname like '%" + conditionstr + "%'";
            }
            else
            {
                ViewState["condition"] = "";
            }

            if (TypeConverter.ObjectToInt(ckstatus.SelectedValue, -1) >= 0)
            {
                string tempc = "status = " + TypeConverter.ObjectToInt(ckstatus.SelectedValue, 0);
                ViewState["condition"] = ViewState["condition"].ToString() == "" ? tempc : ViewState["condition"].ToString() + " AND " + tempc;
            }

            if (TypeConverter.ObjectToInt(ckmain.SelectedValue, -1) > 0)
            {
                string tempc = "CHARINDEX('," + TypeConverter.ObjectToInt(ckmain.SelectedValue, 0) + ",',','+main+',') > 0";
                ViewState["condition"] = ViewState["condition"].ToString() == "" ? tempc : ViewState["condition"].ToString() + " AND " + tempc;
            }

            DataGrid1.CurrentPageIndex = 0;
            BindDataGrid();
        }

        /// <summary>
        /// 重新绑定
        /// </summary>
        protected void reclick_Click(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        /// <summary>
        /// 停止企业合作
        /// </summary>
        protected void pause_Click(object sender, EventArgs e)
        {
            if (SASRequest.GetString("cid") != "")
            {
                Companys.PauseCorp(SASRequest.GetString("cid"));
                BindDataGrid();
            }
        }

        /// <summary>
        /// 开启企业合作
        /// </summary>
        protected void start_Click(object sender, EventArgs e)
        {
            if (SASRequest.GetString("cid") != "")
            {
                Companys.StartCorp(SASRequest.GetString("cid"));
                BindDataGrid();
            }
        }

        #region Web 窗体设计器生成的代码

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            DataGrid1.PageIndexChanged += new DataGridPageChangedEventHandler(DataGrid1_PageIndexChanged);
            DataGrid1.GoToPagerButton.Click += new EventHandler(GoToPagerButton_Click);
            DataGrid1.AllowCustomPaging = true;
            DataGrid1.TableHeaderName = "企业信息列表";
            DataGrid1.Attributes.Add("borderStyle", "0");
            DataGrid1.DataKeyField = "id";
            DataGrid1.ColumnSpan = 12;
            DataGrid1.SaveDSViewState = true;

            check.Click += new EventHandler(check_Click);
            reclick.ServerClick += new EventHandler(reclick_Click);
            pause.Click += new EventHandler(pause_Click);
            start.Click += new EventHandler(start_Click);

            MainEnum menum = new MainEnum();
            ckmain.Items.Clear();
            ckmain.Items.Add(new ListItem("不限", "0"));
            foreach (string mname in Enum.GetNames(menum.GetType()))
            {
                int s_value = Convert.ToInt16(Enum.Parse(menum.GetType(), mname));
                string s_text = LTJ.Entity.EnumType.GetMainEnumType(s_value);
                ckmain.Items.Add(new ListItem(s_text, s_value.ToString()));
            }
        }

        #endregion
    }
}